//
//  ALBaseModel.m
//  ios-loan
//
//  Created by mac on 2018/4/12.
//  Copyright © 2018年 hxhg. All rights reserved.
//

#import "ALBaseModel.h"
#import "YYModel.h"

@implementation ALBaseModel

#pragma mark - 模型转换（public）
+ (instancetype)modelFromJSON:(id)json{
    return [self yy_modelWithJSON:json];
}

- (NSString *)modelToJSONString {
    return [self yy_modelToJSONString];
}

#pragma mark - 序列化和反序列化
- (void)encodeWithCoder:(NSCoder *)aCoder{
    [self yy_modelEncodeWithCoder:aCoder];
}

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder{
    return [self yy_modelInitWithCoder:aDecoder];
}

#pragma mark - 实现copy方法（实现深拷贝）
- (id)copyWithZone:(NSZone *)zone {
    return [self yy_modelCopy];
}

#pragma mark -重写hash、isEqual:和description方法
- (NSUInteger)hash {
    return [self yy_modelHash];
}

- (BOOL)isEqual:(id)object {
    return [self yy_modelIsEqual:object];
}

- (NSString *)description{
    return [self yy_modelDescription];
}
- (instancetype)filteModelByDic:(NSDictionary *)dic allkeys:(NSArray *)properties{
    NSLog(@"所有属性=%@",properties);
    
    [dic.allKeys enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSLog(@"key=%@,val=%@",obj,dic[obj]);
    }];
    return self;
}
- (NSArray *)jl_getProperties {
    //用于存入属性数量
    unsigned int outCount = 0;
    //获取属性数组
    objc_property_t *propertyList = class_copyPropertyList([self class], &outCount);
    
    NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:outCount];
    //遍历数组
    for (int i = 0; i < outCount; ++i) {
        objc_property_t property = propertyList[i];
        //将其转换成c字符串
        NSString *propertyName = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
        //        加入数组
        [arrM addObject:propertyName];
        
        //属性描述
        //属性的特性
        unsigned int attrCount = 0;
        objc_property_attribute_t * attrs = property_copyAttributeList(property, &attrCount);
        for (unsigned int j = 0; j < attrCount; j ++) {
            objc_property_attribute_t attr = attrs[j];
            const char * name = attr.name;
            const char * value = attr.value;
            NSLog(@"属性的描述：%s 值：%s", name, value);
        }
        free(attrs);
    }
    //在使用了c函数的creat, copy等函数是记得手动释放,要不然会引起内存泄露问题
    free(propertyList);
    return arrM.copy;
    
}
@end
